import 'package:logger/logger.dart';

class LyricLine {
  final int timestamp;
  final String text;

  LyricLine({
    required this.timestamp,
    required this.text,
  });
}

class LyricParser {
  static final RegExp _timeExp = RegExp(r'\[\d{2}:\d{2}.\d{2}\]');
  static final RegExp _lyricExp = RegExp(r'\[.*?\](.*)');

  List<LyricLine> parseLyric(String lrcContent) {
    final List<LyricLine> lyricLines = <LyricLine>[];
    final List<String> lines = lrcContent.split('\n');

    for (final String line in lines) {
      final Match? timeMatch = _timeExp.firstMatch(line);
      final Match? lyricMatch = _lyricExp.firstMatch(line);

      if (timeMatch != null && lyricMatch != null) {
        final String timestampStr = timeMatch.group(0)!;
        final String lyricText = lyricMatch.group(1)?.trim() ?? '';
        final int timestamp = _parseTimestamp(timestampStr);

        lyricLines.add(LyricLine(timestamp: timestamp, text: lyricText));
      }
    }

    return lyricLines;
  }

  int _parseTimestamp(String timestampStr) {
    final List<String> parts =
        timestampStr.substring(1, timestampStr.length - 1).split(':');
    final int minutes = int.parse(parts[0]);
    final int seconds = int.parse(parts[1].split('.')[0]);
    final int milliseconds = int.parse(parts[1].split('.')[1]);
    return minutes * 60 * 1000 + seconds * 1000 + milliseconds;
  }
}

void main() {
  final Logger logger = Logger();
  const String lrcContent = '''
  [00:00.00]歌词一
  [00:02.03]歌词二
  [00:04.06]歌词三
  ''';

  final LyricParser parser = LyricParser();
  final List<LyricLine> lyricLines = parser.parseLyric(lrcContent);

  for (final LyricLine line in lyricLines) {
    logger.d('Timestamp: ${line.timestamp}, Lyric: ${line.text}');
  }
}
